package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.equipment.entity.KnowledgeBase;
import com.mxpio.erp.common.equipment.entity.KnowledgeType;
import com.mxpio.erp.common.equipment.service.KnowledgeTypeService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class KnowledgeTypeServiceImpl extends BaseServiceImpl<KnowledgeType> implements KnowledgeTypeService {
    @Override
    @Transactional
    public Result<?> removeByIds(String ids) {
        if (StringUtils.isBlank(ids)){
            return Result.error("入参为空！");
        }
        Set<String> idList = Stream.of(ids.split(",")).collect(Collectors.toSet());
        Set<String> allIds = new HashSet<>();
        for (String id:idList){
            KnowledgeType typeWithSub = getTypeWithSub(id);
            Set<String> subIds = new HashSet<>();
            getIdWithSub(typeWithSub,subIds);
            Long count = JpaUtil.linq(KnowledgeBase.class).in("knowledgeTypeId", subIds).count();
            if (count>0){
                return Result.error("知识分类"+typeWithSub.getTypeName()+"或子类已被知识库使用，不允许删除！");
            }
            allIds.addAll(subIds);
        }
        JpaUtil.lind(KnowledgeType.class).in("typeId",allIds).delete();
        return Result.OK("删除成功");
    }

    private void getIdWithSub(KnowledgeType type,Set<String> idSet){
        idSet.add(type.getTypeId());
        if (type.getChildren()!=null){
            List<KnowledgeType> children = type.getChildren();
            for (KnowledgeType child:children){
                getIdWithSub(child,idSet);
            }
        }
    }
    private KnowledgeType getTypeWithSub(String typeId){
        Map<String, List<KnowledgeType>> childrenMap = new HashMap<String, List<KnowledgeType>>();
        List<KnowledgeType> types = JpaUtil.linq(KnowledgeType.class).list();
        KnowledgeType knowledgeType = null;
        for (KnowledgeType type : types) {
            if (childrenMap.containsKey(type.getTypeId())) {
                type.setChildren(childrenMap.get(type.getTypeId()));
            } else {
                type.setChildren(new ArrayList<KnowledgeType>());
                childrenMap.put(type.getTypeId(), type.getChildren());
            }

            if (type.getParentNodeKey() != null) {
                List<KnowledgeType> children;
                if (childrenMap.containsKey(type.getParentNodeKey())) {
                    children = childrenMap.get(type.getParentNodeKey());
                } else {
                    children = new ArrayList<KnowledgeType>();
                    childrenMap.put(type.getParentNodeKey(), children);
                }
                children.add(type);
            }
            if (type.getTypeId().equals(typeId)){
                knowledgeType = type;
            }
        }
        return knowledgeType;
    }
}
